<template>
  <div class="article-comments">
    <!-- 导航栏 -->
    <van-nav-bar :title=" current_comment.reply_count + '条回复'">
      <van-icon slot="left" name="cross" @click="$emit('close')" />
    </van-nav-bar>
    <!-- /导航栏 -->

    <!-- 当前评论 -->
    <van-cell title="当前评论">
      <van-image
        slot="icon"
        round
        width="30"
        height="30"
        style="margin-right: 10px;"
        :src="current_comment.aut_photo"
      />
      <span slot="title" style="color: #466b9d;">{{ current_comment.aut_name }}</span>
      <div slot="label">
        <p style="color: #363636;">{{ current_comment.content }}</p>
        <p>
          <span style="margin-right: 10px;">{{ current_comment.pubdate | relative }}</span>
          <van-button
            size="mini"
            type="default"
          >回复 {{ current_comment.reply_count }}</van-button>
        </p>
      </div>
      <van-icon slot="right-icon" />
    </van-cell>
    <!-- /当前评论 -->
    <van-divider>全部回复</van-divider>

    <!-- 对当前评论 回复列表 -->
    <van-list
      v-model="loading"
      :finished="finished"
      finished-text="没有更多了"
      @load="onLoad"
    >
      <van-cell
        v-for="(item, index) in list"
        :key="index"
      >
        <van-image
          slot="icon"
          round
          width="30"
          height="30"
          style="margin-right: 10px;"
          :src="item.aut_photo"
        />
        <span slot="title" style="color: #466b9d;">{{ item.aut_name }}</span>
        <div slot="label">
          <p style="color: #363636;">{{ item.content }}</p>
          <p>
            <span style="margin-right: 10px;">{{ item.pubdate | relative }}</span>
          </p>
        </div>
      </van-cell>
    </van-list>
    <!-- 回复列表 -->

    <!-- 发布回复 -->
    <van-cell-group class="publish-wrap">
      <van-field
        v-model="content"
        clearable
        placeholder="请输入回复内容"
      >
        <van-button
          slot="button"
          size="mini"
          type="info"
          @click="addReply"
        >发布</van-button>
      </van-field>
    </van-cell-group>
    <!-- /发布回复 -->
  </div>

</template>
<script>
import { reqGetCommentsReplys, reqAddCommentReply } from '@/api/comment'
export default {
  name: 'CommentReply',
  props: {
    // eslint-disable-next-line vue/prop-name-casing
    current_comment: {
      type: Object,
      required: true
    },
    // eslint-disable-next-line vue/prop-name-casing
    art_id: {
      type: String,
      required: true
    }
  },
  data() {
    return {
      list: [], // 评论列表
      loading: false, // 上拉加载更多的 loading
      finished: false, // 是否加载结束
      offset: null,
      content: ''
    }
  },
  created() {
    console.log(this.current_comment)
  },
  methods: {
    async onLoad() {
      // 异步更新数据
      try {
        const res = await reqGetCommentsReplys(this.current_comment.com_id, this.offset)
        console.log(res)
        const arr = res.data.results
        this.list = [...this.list, ...arr]
        // 加载状态结束
        this.loading = false

        // 数据全部加载完成
        if (arr.length === 0) {
          this.finished = true
        }
        this.offset = res.data.last_id
      } catch (e) {
        console.log(e)
      }
    },

    async addReply() {
      if (this.content.trim === '') return
      const res = await reqAddCommentReply(this.current_comment.com_id, this.content, this.art_id)
      console.log(res)
      this.list.unshift(res.data.new_obj)
      this.current_comment.reply_count++
      this.content = ''
      this.$toast.success('发布成功')
    }
  }
}
</script>
<style lang="scss" scoped>
// 发表评论的区域是固定在下端的
.publish-wrap {
  position: fixed;
  left: 0;
  bottom: 0;
  width: 100%;
}
// 给发表评论区空出地方
.van-list {
  margin-bottom: 45px;
}
</style>
